package com.zlc.common.util;

import com.zlc.common.common.exception.InvalidTokenException;
import com.zlc.common.common.service.ContextHolder;
import com.zlc.common.core.user.model.entity.MayanPlatUser;
import com.zlc.common.core.user.model.vo.MayanPlatUserVo;
import com.zlc.common.core.user.service.MayanPlatUserService;
import com.zlc.common.model.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import static com.zlc.common.common.constant.PlatConstant.LoginConstant.NOW_USER;
import static com.zlc.common.common.constant.PlatformRedisConstant.MAYAN_PLATFORM_LOGIN_USER;

@Component
public class UserHelper {

    private static MayanPlatUserService mayanPlatUserService;

    @Autowired
    public void setMayanPlatUserService(MayanPlatUserService mayanPlatUserService){
        UserHelper.mayanPlatUserService = mayanPlatUserService;
    }

    public static UserInfo getNowUser(){
        UserInfo userInfo = ContextHolder.get(NOW_USER);
        if(ObjectUtils.isEmpty(userInfo)) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token = request.getHeader("token");
            if (ObjectUtils.isEmpty(token)) {
                token = request.getParameter("token");
            }
            if (ObjectUtils.isEmpty(token)) {
                throw new InvalidTokenException("无权限访问");
            }
            String value = RedisUtils.get(MAYAN_PLATFORM_LOGIN_USER + token);
            if (ObjectUtils.isEmpty(value)) {
                throw new InvalidTokenException("无权限访问");
            }
            userInfo = JsonHelper.jsonToObject(value, UserInfo.class);
            ContextHolder.set(NOW_USER,userInfo);
        }
        return userInfo;
    }

    public static void refreshUserInfo(UserInfo userInfo){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if (ObjectUtils.isEmpty(token)) {
            token = request.getParameter("token");
        }
        if (ObjectUtils.isEmpty(token)) {
            throw new InvalidTokenException("无权限访问");
        }
        RedisUtils.set(MAYAN_PLATFORM_LOGIN_USER + token,JsonHelper.toJson(userInfo));
    }

    public static String getNowAccount(){
        return getNowUser().getUserAccount();
    }

    public static MayanPlatUserVo getUser(String account){
        return mayanPlatUserService.queryByUserAccount(account);
    }


    public static String  getUserName(String account){
        MayanPlatUserVo user = getUser(account);
        if(!ObjectUtils.isEmpty(user)){
            return user.getUsername();
        }
        return null;
    }

}
